package com.example;

/**
 * Created by IntelliJ IDEA.
 * User: Ranju Phelan
 * Date: 7/9/11
 * Time: 3:26 PM
 * To change this template use File | Settings | File Templates.
 */

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;




public class MyActivity extends Activity {

    SitesList sitesList = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {

            /** Handling XML */
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();

            InputSource is = new InputSource(getResources().openRawResource(R.raw.example));

            /** Create handler to handle XML Tags ( extends DefaultHandler ) */
            MyXMLHandler myXMLHandler;
            myXMLHandler = new MyXMLHandler();
            xr.setContentHandler(myXMLHandler);
            xr.parse(new InputSource(is.getByteStream()));

        } catch (Exception e) {
            System.out.println("XML Parsing Exception = " + e);
        }

        sitesList = MyXMLHandler.sitesList;

        final TextView quizQuestionNo = (TextView)    findViewById(R.id.questionNo);
        final TextView quizQuestion   = (TextView)    findViewById(R.id.question);

        final RadioButton rbAnswer1   = (RadioButton) findViewById(R.id.rbAnswer1);
        final RadioButton rbAnswer2   = (RadioButton) findViewById(R.id.rbAnswer2);
        final RadioButton rbAnswer3   = (RadioButton) findViewById(R.id.rbAnswer3);
        final RadioButton rbAnswer4   = (RadioButton) findViewById(R.id.rbAnswer4);


        this.askQuestion("Initial", quizQuestionNo, quizQuestion, rbAnswer1, rbAnswer2, rbAnswer3, rbAnswer4, 0);


        ImageButton nextButton,prevButton,finishButton;
        prevButton   = (ImageButton) this.findViewById(R.id.btnPrev);
        nextButton   = (ImageButton) this.findViewById(R.id.btnNext);
        finishButton = (ImageButton) this.findViewById(R.id.btnFinish);

        prevButton.setOnClickListener(new View.OnClickListener() {
            // @Override
            public void onClick(View v) {
                int iCurrentQuestion = sitesList.question_text.indexOf(quizQuestion.getText());
                int iQuestionCount = sitesList.getQuestionText().size();

                if ((iCurrentQuestion <= iQuestionCount) & (iCurrentQuestion != 0 )){
                    askQuestion("Previous", quizQuestionNo, quizQuestion, rbAnswer1, rbAnswer2, rbAnswer3, rbAnswer4, iCurrentQuestion - 1);
                }
            }
        });
        nextButton.setOnClickListener(new View.OnClickListener() {
            // @Override
            public void onClick(View v) {
                int iCurrentQuestion = sitesList.question_text.indexOf(quizQuestion.getText());
                int iQuestionCount = sitesList.getQuestionText().size();

                if (iCurrentQuestion < (iQuestionCount - 1)){
                    askQuestion("Next", quizQuestionNo, quizQuestion, rbAnswer1, rbAnswer2, rbAnswer3, rbAnswer4, iCurrentQuestion + 1);
                }
            }
        });

        final AlertDialog alertDialog;
        alertDialog = new AlertDialog.Builder(this).create();

        finishButton.setOnClickListener(new View.OnClickListener() {

            // @Override
            public void onClick(View v) {
                int iQuestionCount = sitesList.getQuestionText().size();

                int iCorrect = 0;
                int iWrong = 0;
                int iUnanswered = 0;

                String sCorrectAnswer;
                String sActualAnswer;
                String sUnAnswer;


                for (int i=0; i < iQuestionCount; i++){

                    sCorrectAnswer = sitesList.question_answer.get(i);
                    sActualAnswer  = sitesList.question_actual.get(i);

                    if (sActualAnswer.equalsIgnoreCase(sCorrectAnswer)) {
                        iCorrect = iCorrect + 1;
                    }
                    else if (sActualAnswer.equalsIgnoreCase("Unanswered")){
                        iUnanswered = iUnanswered + 1;
                    }
                    else {
                        iWrong = iWrong + 1;
                    }

                }

                alertDialog.setTitle("Results");
                alertDialog.setMessage("Correct: "    + Integer.toString(iCorrect)  + "\n" +
                                       "Wrong: "      + Integer.toString(iWrong)    + "\n" +
                                       "Unanswered: " + Integer.toString(iUnanswered));
                alertDialog.show();
            }


        });

        rbAnswer1.setOnClickListener(radio_listener);
        rbAnswer2.setOnClickListener(radio_listener);
        rbAnswer3.setOnClickListener(radio_listener);
        rbAnswer4.setOnClickListener(radio_listener);



    }

     @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
        // outState.putString("TEXT", (String)text.getText());
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onRestoreInstanceState(savedInstanceState);
        // text.setText(savedInstanceState.getString("TEXT"));
    }


    public void askQuestion(String sQuestionType,TextView tv1,TextView tv2, RadioButton rb1,RadioButton rb2,RadioButton rb3,RadioButton rb4,int iQuestNum) {
        int iQuestionCount = sitesList.getQuestionText().size();


        String sQuestionNo = "";
        if (sQuestionType.equalsIgnoreCase("Initial")){
            sQuestionNo = "Question " + Integer.toString(iQuestNum + 1) + " of " + Integer.toString(iQuestionCount);
        }
        else if (sQuestionType.equalsIgnoreCase("Previous")){
            sQuestionNo = "Question " + Integer.toString(iQuestNum + 1 ) + " of " + Integer.toString(iQuestionCount);
        }
        else if (sQuestionType.equalsIgnoreCase("Next")){
            sQuestionNo = "Question " + Integer.toString(iQuestNum + 1) + " of " + Integer.toString(iQuestionCount);
        }


        tv1.setText(sQuestionNo);
        tv2.setText(sitesList.getQuestionText().get(iQuestNum));
        rb1.setText(sitesList.getQuestion_option1().get(iQuestNum));
        rb2.setText(sitesList.getQuestion_option2().get(iQuestNum));
        rb3.setText(sitesList.getQuestion_option3().get(iQuestNum));
        rb4.setText(sitesList.getQuestion_option4().get(iQuestNum));

        this.setRadioGroupState(iQuestNum);
    }


    private void setRadioGroupState(int iQuestNum){

        final RadioGroup rgAnswers    = (RadioGroup) findViewById(R.id.rgAnswers);
        RadioButton rbAnswer1   = (RadioButton) findViewById(R.id.rbAnswer1);
        RadioButton rbAnswer2   = (RadioButton) findViewById(R.id.rbAnswer2);
        RadioButton rbAnswer3   = (RadioButton) findViewById(R.id.rbAnswer3);
        RadioButton rbAnswer4   = (RadioButton) findViewById(R.id.rbAnswer4);

        TextView question_TextView = (TextView) findViewById(R.id.questionNo);

        rgAnswers.clearCheck();
        question_TextView.setBackgroundColor(0xAFBB7B);

        String sAnswer = sitesList.getQuestionActual().get(iQuestNum);

        if (rbAnswer1.getText() == sAnswer) {
            rgAnswers.check(R.id.rbAnswer1);
            if (rbAnswer1.getText().toString().equalsIgnoreCase(sitesList.question_answer.get(iQuestNum))) {
                question_TextView.setBackgroundColor(Color.GREEN);
            }
            else {
                question_TextView.setBackgroundColor(Color.RED);
            }
        }
        else if (rbAnswer2.getText() == sAnswer){
            rgAnswers.check(R.id.rbAnswer2);
            if (rbAnswer2.getText().toString().equalsIgnoreCase(sitesList.question_answer.get(iQuestNum))) {
                question_TextView.setBackgroundColor(Color.GREEN);
            }
            else {
                question_TextView.setBackgroundColor(Color.RED);
            }
        }
        else if ((rbAnswer3.getText() == sAnswer)){
            rgAnswers.check(R.id.rbAnswer3);
            if (rbAnswer3.getText().toString().equalsIgnoreCase(sitesList.question_answer.get(iQuestNum))) {
                question_TextView.setBackgroundColor(Color.GREEN);
            }
            else {
                question_TextView.setBackgroundColor(Color.RED);
            }
        }
        else if ((rbAnswer4.getText() == sAnswer)){
            rgAnswers.check(R.id.rbAnswer4);
            if (rbAnswer4.getText().toString().equalsIgnoreCase(sitesList.question_answer.get(iQuestNum))) {
                question_TextView.setBackgroundColor(Color.GREEN);
            }
            else {
                question_TextView.setBackgroundColor(Color.RED);
            }
        }


    }


    RadioButton.OnClickListener radio_listener;

    {
        radio_listener = new RadioButton.OnClickListener() {

            @Override
            public void onClick(View v) {
                TextView quizQuestion = (TextView) findViewById(R.id.question);
                int iCurrentQuestion = sitesList.question_text.indexOf(quizQuestion.getText());

                RadioButton rb = (RadioButton) v;
                String sActual = rb.getText().toString();
                if (rb.isChecked()) {
                    sitesList.question_actual.set(iCurrentQuestion, sActual);

                    TextView question_TextView = (TextView) findViewById(R.id.questionNo);

                    if (sActual.equalsIgnoreCase(sitesList.question_answer.get(iCurrentQuestion))) {
                        question_TextView.setBackgroundColor(Color.GREEN);
                    }
                    else {
                        question_TextView.setBackgroundColor(Color.RED);
                    }



                }


            }
        };
    }


}


